package util;


import java.sql.Connection;
import java.sql.DriverManager;

/**
 * @author: jack
 * @date: 2018/12/15 11:5907
 * @version: 0.1
 * @description: <文件的作用>
 * @modified: <修改说明>
 **/


public class DBHelper {

    private static final String driver = "com.mysql.jdbc.Driver"; //数据库驱动
    //连接数据库的URL地址
    private static final String url="jdbc:mysql://47.96.38.227:3306/xll_001?useUnicode=true&characterEncoding=UTF-8";
    private static final String username="xll";//数据库的用户名
    private static final String password="xll@Pwd123!!!";//数据库的密码

    private static Connection conn=null;

    //静态代码块负责加载驱动
    static
    {
        try
        {
            Class.forName(driver);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }

    //单例模式返回数据库连接对象
    public static Connection getConnection() throws Exception
    {
        if(conn==null)
        {
            conn = DriverManager.getConnection(url, username, password);
            return conn;
        }
        return conn;
    }

    public static void main(String[] args) {

        try
        {
            Connection conn = DBHelper.getConnection();
            if(conn!=null)
            {
                System.out.println("数据库连接正常！");
            }
            else
            {
                System.out.println("数据库连接异常！");
            }
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }

    }
}

